package a0.regularExpression;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegDemo {

    /**
     * 判断字符串是否为数组组成
     */
    @Test
    public void test() {

        String s = "20190127";

        char[] chars = s.toCharArray();
        boolean flag = true;
        for (char aChar : chars) {
            if (aChar < '0' || aChar > '9') {
                flag = false;
                break;
            }
        }

        if (flag) {
            System.out.println("是全部由数字组成");
        } else {
            System.out.println("不是全部由数字组成");

        }

    }


    @Test
    public void test2() {
        //创建一个匹配模板
        Pattern pattern = Pattern.compile("a*b");
        //闯入输入字符
        Matcher matcher = pattern.matcher("aaaab");
        //匹配
        boolean matches = matcher.matches();

        System.out.println(matches);
    }

    @Test
    public void test1() {

        String s = "20190127";

        boolean matches = s.matches("[0-9]+");
        boolean matches1 = s.matches("\\d+");

        System.out.println(matches);
        System.out.println(matches1);
    }

    /**
     * 案例
     * 更多案例:https://www.cnblogs.com/fozero/p/7868687.html
     */
    @Test
    public void test3() {

        //匹配电话号码
        String phoneNumber = "0371-8265004";

        boolean phoneNumberMatches = phoneNumber.matches("^\\d{3,4}-\\d{7,8}$");
        System.out.println("phoneNumberMatches:  " + phoneNumberMatches);

        //匹配手机号码
        String phone = "15737976389";

        boolean phoneMatches = phone.matches("^[1][3-9]\\d{9}$");
        System.out.println("phoneMatches:  " + phoneMatches);

        //匹配用户名：以字母开头，数字字母下划线组合,长度不小于6位不大于12位
        String username = "ad224_222";

        boolean usernameMatches = username.matches("^[a-zA-Z][\\w|_]{5,11}$");
        System.out.println("usernameMatches:  " + usernameMatches);

        //匹配IP地址
        String ip = "192.168.02.45";

        boolean ipMatches = ip.matches("^\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}$");
        System.out.println("ipMatches:  " + ipMatches);

        //匹配网址
        String netAddr = "http://www.baidu.com";
        String reg = "^http[s]?://\\w.\\w.\\S*$";
        boolean netAddrMatches = netAddr.matches(reg);
        System.out.println("netAddrMatches:  " + netAddrMatches);


    }

    public static void main(String[] args) {
        System.out.println("\"");
        System.out.println("\\t");
        System.out.println("\t");
    }

}
